package com.bw.usercenter.ui

import android.annotation.SuppressLint
import android.database.sqlite.SQLiteDatabase
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import com.bw.common.sp.SPPropDelegate
import com.bw.common.sp.SPUtils
import com.bw.mvpcore.view.BaseMVPActivity
import com.bw.usercenter.R
import com.bw.usercenter.contract.UserCenterContract
import com.bw.usercenter.model.protocol.req.UserEntitiy
import com.bw.usercenter.model.protocol.resp.RespRecommendEntity
import com.bw.usercenter.model.protocol.resp.RespSearchEntity
import com.bw.usercenter.model.protocol.resp.RespSearchitemIDEntity
import com.bw.usercenter.model.protocol.resp.UserBean
import com.bw.usercenter.presenter.UserCenterPresenterImpl
import com.bw.usercenter.sq.MySQ
import kotlinx.android.synthetic.main.activity_log.*
import kotlinx.android.synthetic.main.activity_log_pass.*
import kotlinx.android.synthetic.main.activity_reg.*
import org.jetbrains.anko.internals.AnkoInternals.createAnkoContext
import org.jetbrains.anko.toast

class LogActivity : BaseMVPActivity<UserCenterPresenterImpl>(),UserCenterContract.UserCenterView {

    private var username:String by SPPropDelegate("username","")
    private var userpass:String by SPPropDelegate("userpass","")
    var db: SQLiteDatabase?=null

    override fun getLayoutId(): Int {
        return R.layout.activity_log
    }
    @SuppressLint("NewApi")
    override fun initEvent() {
        var mySQ: MySQ = MySQ(this@LogActivity)
        db = mySQ.writableDatabase as SQLiteDatabase
        regbtnok.setOnClickListener {
            var name = reg_username.text.toString()
            var pass = reg_userpass.text.toString()
            if (TextUtils.isEmpty(name)&&TextUtils.isEmpty(pass)){
                toast("账号或密码不能为空!")
            }else{
                val boolean:Boolean=MyQueryUser(name,pass)
                if (boolean){
                    mPresenter.login(UserEntitiy("2021-01-01",0,pass,"1",name))
                }else{
                    toast("用户名或密码错误!请重新登录.....")
                    println("$username---$userpass")
                }
            }
        }

        reguser.setOnClickListener {
            jumpActivity(RegActivity::class.java)
        }
        yzcode.setOnClickListener {
            jumpActivity(LogActivityPass::class.java)
        }
    }
    fun MyQueryUser(name:String,pass:String):Boolean{
        var userlise:ArrayList<UserBean> = arrayListOf()
        var bool:Boolean = false
        val cursor= db!!.query("user",null,null,null,null,null,null)
        if (cursor.moveToFirst()){
            while (cursor.moveToNext()){
                val user = UserBean()
                user.name = cursor.getString(cursor.getColumnIndex("name"))
                user.pass = cursor.getString(cursor.getColumnIndex("pass"))
                user.sex = cursor.getString(cursor.getColumnIndex("sex"))
                println("User:--账号:${user.name}--密码:${user.pass}--性别:${user.sex}")
                userlise.add(user)
            }
        }
        for (time in userlise){
            if (name.equals(time.name)&&pass.equals(time.pass)){
                    bool=true
                }
        }
        if (bool){
            return bool
        }
        return bool
    }


    override fun ClearStatBar() {

    }

    override fun initData(savedInstanceState: Bundle?) {

    }
    override fun createPresenter(): UserCenterPresenterImpl {
        return UserCenterPresenterImpl(this)
    }


    override fun releaseResource() {

    }

    override fun <T> registerSuccess(data: T) {
        toast("登录成功")
        jumpActivity(MainActivity::class.java)

    }

    override fun registerSuccess(data: RespRecommendEntity) {

    }

    override fun registerFaild(throwable: Throwable) {
        toast("登录失败!")
    }

    override fun registerSrech(respSearchEntity: RespSearchEntity) {

    }

    override fun getSearchitem(respSearchitemIDEntity: RespSearchitemIDEntity) {

    }

}